<?php

namespace App\Http\Controllers\Admin;

use \App\Http\Controllers\Controller;
use App\Admin\Classification;	//引入admin模型
use Input;

class ClassificationController extends Controller
{
    public function index(){
        $data = Classification::get();
        return view('admin.classification.index',['data' => $data]);
    }

    public function add(){
        if(Input::method() == 'POST'){
            $data   = Input::all();
            $result = Classification::insert([
                'classification_name' => $data['classification_name'] ? $data['classification_name'] : '',
                'cover_img'		      => $data['cover_img']  ? $data['cover_img'] : '',
                'banner_img'          => $data['banner_img'] ? $data['banner_img'] : '',
                'carousel_img'        => $data['carousel_img'] ? $data['carousel_img'] : '',
                'created_at'	      => date('Y-m-d H:i:s'),
                'sort'			      => $data['sort'] ? $data['sort'] : 0,
                'status'              => 2,
            ]);
            session(['cover_img_url' => null]);
            session(['banner_img_url' => null]);
            session(['carousel_img_url' => null]);
            return $result ? '1' : '0';
        }else{
            return view('admin.classification.add');
        }
    }

    public function edit(){
        if(Input::method() == 'POST'){
            $data   = Input::all();
            $result = Classification::where('id',$data['id']) -> update([
                'classification_name' => $data['classification_name'] ? $data['classification_name'] : '',
                'cover_img'		      => $data['cover_img']  ? $data['cover_img'] : '',
                'banner_img'          => $data['banner_img'] ? $data['banner_img'] : '',
                'carousel_img'        => $data['carousel_img'] ? $data['carousel_img'] : '',
                'created_at'	      => date('Y-m-d H:i:s'),
                'sort'			      => $data['sort'] ? $data['sort'] : 0,
                'status'              => $data['status'] ? $data['status'] : 2,
            ]);
            session(['cover_img_url' => null]);
            session(['banner_img_url' => null]);
            session(['carousel_img_url' => null]);
            return $result ? '1' : '0';
        }else{
            $id    = Input::get('id');
            $data  = Classification::where('id',$id) -> first();
            return view('admin.classification.edit',['data' => $data]);
        }
    }

    public function edit_status(){
        $id = Input::get('id');
        $status = Input::get('status');
        $result = Classification::where('id',$id) -> update([
            'status' => $status
        ]);
        return $result ? '1' : '0';
    }

    public function del(){
        $id     = Input::get('id');
        $result = Classification::where('id',$id) -> delete();
        return $result ? '1' : '0';
    }

    public function search(){
        $keyword    = Input::get('key');
        $start_time = Input::get('datemin') ? Input::get('datemin') . ' 00:00:00' : '1999-01-01 00:00:00';
        $end_time   = Input::get('datemax')? Input::get('datemax') . ' 23:59:59' : date('Y-m-d H:i:s');
        $data       = Classification::where('classification_name','LIKE',"%$keyword%") -> whereBetween('created_at',[$start_time,$end_time]) -> get();
        return view('admin.classification.index',['data' => $data, 'key' => $keyword ,'datemin' => Input::get('datemin') , 'datemax' => Input::get('datemax')]);
    }

}